<?php
require_once 'TVB/MSApi/Client/requires.php';
require_once 'TVB/Logger/TVBLog.php';
TVBLog::config("/data/logger/conf/config.xml");

require_once ( 'TVB/Entity/MemberFavourEntity.php' ) ;

$action = array_key_exists('action', $_REQUEST)?$_REQUEST['action']:'view' ;
$url = array_key_exists ('url', $_REQUEST)?$_REQUEST['url']:'' ;
$video_title = array_key_exists('video_title', $_REQUEST)?$_REQUEST['video_title']:'' ;
$signature = array_key_exists('member_id', $_REQUEST)?$_REQUEST['member_id']:'' ;
if ( empty($signature )) echo $_GET['jsonp_callback'].'('.json_encode(array('status'=>0, 'error_code'=>'00', 'description' => 'invalid signature.')).')';

$obj = new MSApiClient();
$member_id = base64_encode(urldecode($signature));
$result = $obj->restRequest('Member','checkLoginByCookie', array('cookie' => $member_id));
$login_id = (empty($result->result->login_id)) ? 'guest' : (string)$result->result->login_id ;



if ( $login_id == 'guest' ) {
	$tmp='<ul class="mytvplus-my-playlist-items">';
	$tmp = '</ul>' ;
	$data2 = json_encode($tmp);
	echo $_GET['jsonp_callback'] . '(' . $data2 . ');';
	return ;
}

$entity = new MemberFavourEntity($login_id) ;
if ( $action == 'add' ) {
	$url = array_key_exists ('url', $_REQUEST)?$_REQUEST['url']:'' ;
	$video_title = array_key_exists('video_title', $_REQUEST)?$_REQUEST['video_title']:'' ;
	$video_id = array_key_exists('video_id',$_REQUEST)?$_REQUEST['video_id']:'' ;
	$programme_id = array_key_exists('programme_id', $_REQUEST)?$_REQUEST['programme_id']:'' ;

	$ret = $entity->selectByQuery("select * from favour where url = '".$url."'") ;
	//if ( !empty ( $ret )) { echo $_GET['jsonp_callback'].'('.json_encode(array('status'=>0, 'error_code'=>'01', 'description'=>'favour already exists.')).')'; return ; }


    $sample_obj = array ('datetime' => '', 'url' => '', 'video_title' => '', 'video_id' => '', 'programme_id' => '') ;
 
    $entity_record = array_intersect_key( $_REQUEST, $sample_obj ) ;
	$entity_record['datetime'] = time() ; 
    $favour_entity = MemberFavourEntity::getInstance( $login_id ) ; 
    $favour_entity->insert ( $entity_record ) ;

	//echo json_encode(array('status'=>1));

	$result = array ( 'status' => 1, 'description' => 'add favour success') ;
	echo $_GET['jsonp_callback'] . '(' . json_encode($result) . ');';

	return ;	
	// add favour
} else if ($action == 'delete') {
	$video_id = array_key_exists('video_id',$_REQUEST)?$_REQUEST['video_id']:'' ;
	$query = "delete from favour where video_id = '".$video_id."'" ;	
	$entity->exec( $query ) ;
	$result = array ( 'status' => 1, 'description' => 'remove favour success') ;
	echo $_GET['jsonp_callback'] . '(' . json_encode($result) . ');';
	return ;
} else if ( $action == 'delete_all') {
	$query = "delete from favour" ;	
	$entity->exec( $query ) ;
	$result = array ( 'status' => 1, 'description' => 'remove favour success') ;
	echo $_GET['jsonp_callback'] . '(' . json_encode($result) . ');';
	return ;

} else {
	// view favour
	$data = $entity->selectByQuery("select * from favour where video_title != '' group by url order by datetime desc") ;

	$tmp='<ul class="mytvplus-my-playlist-items">';
	foreach ($data as $favour ) {
		$expire = (array_key_exists('expired', $favour) && $favour['expired']) ?  'expired' : '';
		$free = (array_key_exists('free', $favour) && $favour['free']) ? ' free' : '';
		$tmp = $tmp.'<li class="'.$expire.$free.'"><a class="remove" href="#" rel="fav-'.$favour['video_id'].'">-</a><h5><a href="'.$favour['url'].'">'.$favour['video_title'].'</a></h5></li>';
	}
	$tmp = $tmp. '</ul>';

	$data2 = json_encode($tmp);
	echo $_GET['jsonp_callback'] . '(' . $data2 . ');';
}
?>
